import accessEnum from "@/access/AccessEnum";
import router from "@/router";
import store from "@/store";
import checkAccess from "./CheckAccess";

router.beforeEach(async (to, from, next) => {
  const loginUser = store.state.user.loginUser;
  if (!loginUser || !loginUser.userRole) {
    await store.dispatch("user/getLoginUser");
  }

  const needAccess = (to.meta?.access as string) ?? accessEnum.NOT_LOGIN;
  if (needAccess !== accessEnum.NOT_LOGIN) {
    if (!loginUser || !loginUser.userRole) {
      next(`/user/login?redirect=${to.fullPath}`);
      return;
    }

    if (!checkAccess(loginUser, needAccess)) {
      next("/noAuth");
      return;
    }
  }

  next();
});
